package com.ow.dva.module.account.service.impl;

import cn.hutool.crypto.digest.MD5;
import com.ow.dva.module.account.entity.Account;
import com.ow.dva.module.account.entity.AccountInfo;
import com.ow.dva.module.account.mapper.AccountInfoMapper;
import com.ow.dva.module.account.mapper.AccountMapper;
import com.ow.dva.module.account.service.AccountInfoService;
import com.ow.dva.module.base.service.impl.DvaServiceImpl;
import com.ow.dva.module.city.entity.City;
import com.ow.dva.module.city.mapper.CityMapper;
import com.ow.dva.module.permission.entity.AccountRole;
import org.springframework.stereotype.Service;
import com.ow.dva.module.base.entity.param.DvaPage;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 高祥
 * @since 2020-01-09
 * @version V1.0
 */
@Service
public class AccountInfoServiceImpl extends DvaServiceImpl<AccountInfoMapper, AccountInfo> implements AccountInfoService {

    @Resource
    AccountMapper accountMapper;

    @Override
    public DvaPage<AccountInfo> page(DvaPage<AccountInfo> page, Map<String, Object> param) {
        DvaPage<AccountInfo> dvaPage = baseMapper.page(page, param);
        if(dvaPage.getRecords() != null && !dvaPage.getRecords().isEmpty()){
            for (AccountInfo accountInfo:dvaPage.getRecords()) {
                if (accountInfo.getAccountId() != null){
                    Account account = accountMapper.selectById(accountInfo.getAccountId());
                    if(account != null){
                        accountInfo.setUserName(account.getUserName());
                    }
                }
            }
        }
        return dvaPage;
    }

    @Override
    public AccountInfo findByAccountId(String accountId) {
        return baseMapper.findByAccountId(accountId);
    }

    //保存值班人员
    public boolean createDutyManager(AccountInfo accountInfo) {
        accountInfo.setCreateTime(new Date());
        baseMapper.insert(accountInfo);
        return true;
    }

    //保存停车场管理员
    @Override
    public boolean createParkAccount(AccountInfo accountInfo) {
        //保存登录帐号
        Account account = new Account(accountInfo.getUserName(), MD5.create().digestHex(accountInfo.getPassword()),accountInfo.getName(),accountInfo.getManagerType(),1);
        int insert = accountMapper.insert(account);

        if(insert > 0){
            //给停车场管理员赋予普通管理员角色
//            AccountRole accountRole = new AccountRole(account.getId(),"888889");
//            accountRoleMapper.insert(accountRole);

            //地区名称
            selectCityInfo(accountInfo);
            //保存停车场管理员详情
            accountInfo.setType(2);
            accountInfo.setAccountId(account.getId());
            accountInfo.setCreateTime(new Date());
            baseMapper.insert(accountInfo);
            return true;
        }else{
            return false;
        }
    }

    /**
     * 描述： 地区名称查询
     * 创建时间：2020/5/11  YanZengBao
     * 修改时间：2020/5/11  YanZengBao 加注释
     * 修改时间：
     */
    private void selectCityInfo(AccountInfo accountInfo){
        City province = cityMapper.selectById(accountInfo.getProvince());
        City city = cityMapper.selectById(accountInfo.getCity());
        City county = cityMapper.selectById(accountInfo.getCounty());
        if(province != null) accountInfo.setProvince(province.getName());
        if(city != null) accountInfo.setCity(city.getName());
        if(county != null) accountInfo.setCounty(county.getName());
    }

    @Resource
    CityMapper cityMapper;
}
